<template>
  <div>
    <!-- 列表页面 -->
    <div class="container" v-if="!showEdit">
      <div class="header">
        <div class="title" style=" text-align: left;">广告图片列表</div>
      </div>
      <el-table :data="tableData" border style="width: 120%">
        <el-table-column align="center" prop="id" label="图片id" width="100">
        </el-table-column>
        <el-table-column prop="url" label="广告图片" width="220">
          <template v-if="scope.row.url" slot-scope="scope">
            <el-image :src="scope.row.url" :preview-src-list="imgSrcList" style="max-height: 50px; max-width: 100px;">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="priority" label="展示优先级" width="300">
        </el-table-column>
        <el-table-column align="center" prop="state" label="是否启用" :show-overflow-tooltip="true" width="450">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              :disabled="true"
              :active-value="1"
              :inactive-value="0"
              active-text="启用"
              inactive-text="禁用"
              @change="changeSwitch(scope.row.state)"
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button @click.prevent="handleEdit(scope.row)" type="primary" plain size="mini">编辑</el-button>
            <el-button @click.prevent="handleDelete(scope.row)" type="danger" plain size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination @current-change="handleCurrentChange" :background="true" :page-size="pageCount"
                       :current-page="currentPage" v-if="refreshPagination" layout="prev, pager, next, jumper" :total="totalNums">
        </el-pagination>
      </div>
    </div>
    <!-- 编辑页面 -->
    <advpic-edit v-else @editClose="editClose" :advPicId="advPicId"></advpic-edit>
  </div>
</template>

<script>
import {
  Advertising
} from '@/model/advertising'
import AdvpicEdit from '@/view/advertising/advpic-edit'

export default {
  components: {
    AdvpicEdit,
  },
  data() {
    return {
      showEdit: false,
      tableData: [],
      totalNums: 0,
      currentPage: 1,
      pageCount: 10,
      refreshPagination: true,
      imgSrcList: [], // 用于大图预览
      advPicId: 0,
    }
  },
  async created() {
    this.loading = true
    this.getAll()
    this.loading = false
  },
  methods: {
    async getAll() {
      const page = this.currentPage - 1
      const count = this.pageCount
      const infos = await Advertising.pages(page, count)
      this.tableData = infos.items
      this.totalNums = infos.total
      this.initImgSrcList();
    },
    // 切换table页
    async handleCurrentChange(val) {
      this.currentPage = val
      this.loading = true
      this.getAll()
      this.loading = false
    },
    initImgSrcList() {
      this.tableData.forEach(item => {
        if (!item.url) {
          return
        }
        this.imgSrcList.push(item.url)
      })
    },
    handleEdit(val) {
      this.showEdit = true
      this.advPicId = val.id
    },
    handleDelete(val) {
      this.$confirm('是否删除该项?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        const res = await Advertising.delete(val.id)
        if (res.code < window.MAX_SUCCESS_CODE) {
          this.getAll()
          this.$message({
            type: 'success',
            message: `${res.message}`,
          })
        }
      })
    },
    rowClick() { },
    editClose() {
      this.showEdit = false
      this.getAll()
    },
  },
}
</script>

<style lang="scss" scoped>
.container {
  padding: 0 30px;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .title {
      height: 59px;
      line-height: 59px;
      color: $parent-title-color;
      font-size: 16px;
      font-weight: 500;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
    margin: 20px;
  }
}
</style>
